﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Common;

namespace CIT.WMS.Msd
{
    public partial class MSD_Out_Baking : Form
    {
        DataTable dtMsd = new DataTable();

        string expose_time_length = string.Empty;

        public MSD_Out_Baking()
        {
            InitializeComponent();
        }

        private void txtSerialNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string msg = string.Empty;
                if (!ValidateInput(out msg))
                {
                    PublicUtils.ShowMsg(this.lab_msg, msg, false);
                    txtSerialNumber.SelectAll();
                    txtSerialNumber.Focus();
                    return;
                }
                PublicUtils.WriteLog(txtSerialNumber.Text.Trim(), "出烘烤", ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["MaterialCode"]), ConvertUtils.ChangeNullToInt(dtMsd.Rows[0]["QTY"], 0));
                PublicUtils.ShowMsg(this.lab_msg, msg, true);
                txtSerialNumber.SelectAll();
                txtSerialNumber.Focus();
            }
        }

        private bool ValidateInput(out string msg)
        {
            if (string.IsNullOrEmpty(txtSerialNumber.Text.Trim()))
            {
                msg = "ReelID不能为空";
                return false;
            }
            dtMsd = MSDService.Query(string.Format(" AND a.SerialNumber='{0}'", txtSerialNumber.Text.Trim()));
            if (dtMsd.Rows.Count == 0)//校验库存表是否存在该条码
            {
                msg = "ReelID错误";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) == "6")//校验是否重复出烘烤
            {
                msg = "ReelID请勿重复出烘烤";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["IsMSD"]) != "0")//校验是否是MSD
            {
                msg = "ReelID非MSD";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) != "5")//2.不是烘烤状态的物料MSD不允许通过
            {
                msg = "ReelID非烘烤状态";
                return false;
            }
            //计算烘烤时长
            expose_time_length = string.Empty;
            DataTable dtLength = MSDService.CalBakeTimeLength(txtSerialNumber.Text.Trim());
            if (dtLength.Rows[0][0].ToString() == "0")
            {
                msg = "ReelID数据异常";
                return false;
            }
            else
            {
                //1.实际烘烤时间未达到设定的烘烤时间，物料不允许结束烘烤
                expose_time_length = dtLength.Rows[0]["bake_time_length"].ToString();//实际烘烤时长(分钟)
                string db_pre_expose_length = dtMsd.Rows[0]["RoastTime"].ToString();//数据库维护的烘烤时长(小时)
                if (ConvertUtils.ChangeNullToInt(expose_time_length, 0) < (ConvertUtils.ChangeNullToInt(db_pre_expose_length, 0) * 60))
                {
                    msg = "实际烘烤时间未达到设定的烘烤时间，物料不允许结束烘烤";
                    return false;
                }
                MSDService.Out_Baking(txtSerialNumber.Text.Trim(), expose_time_length);
                msg = "出烘烤成功";
                return true;
            }
        }
    }
}